package com.cie2.map.services;

import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;

import com.cie2.map.entities.Area;
import com.cie2.map.entities.GMarkers;

public class JSONProcessor {

	public JSONArray marshal(List areaMarkers) {
		JSONArray jsonArray = new JSONArray();
		
		for (int i = 0; i < areaMarkers.size(); i++) {
			List markers = (List) areaMarkers.get(i);
			GMarkers markerForArea = (GMarkers) markers.get(0);
			Area area = markerForArea.getArea();

			JSONObject areaJSON = new JSONObject();
			areaJSON.put("name", area.getName());
			areaJSON.put("defaultAreaColor", area.getDefaultAreaColor());
			areaJSON.put("defaultLineColor", area.getDefaultLineColor());
			
			JSONArray markerArray = new JSONArray();
			for (Iterator iterator = markers.iterator(); iterator.hasNext();) {
				GMarkers gmarker = (GMarkers) iterator.next();
				markerArray.put(gmarker.getLat());
				markerArray.put(gmarker.getLng());			
			}
			areaJSON.put("markers", markerArray);			
			jsonArray.put(areaJSON);
		}
		
		return jsonArray;
	}
	
	public void unmarshal() {
		
	}
}
